package io.renren.sf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.sf.entity.CbBrushEntity;
import io.renren.sf.entity.CbStandardBrushEntity;
import io.renren.sf.entity.CbTypefaceEntity;
import io.renren.sf.service.CbBasisStrokesFormworkService;
import io.renren.sf.service.CbBrushService;
import io.renren.sf.service.CbStandardBrushService;
import io.renren.sf.service.CbTypefaceService;
import io.renren.sf.vo.FileNameAndUrl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 *
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-03-31 13:04:12
 */
@RestController
@RequestMapping("sf/cbbrush")
@Api(tags = "笔法")
@ApiIgnore
public class CbBrushController {
    @Autowired
    private CbBrushService cbBrushService;
    @Autowired
    private CbStandardBrushService cbStandardBrushService;

    @Autowired
    private CbTypefaceService cbTypefaceService;

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbbrush:info")
    public R info(@PathVariable("id") Long id){
		CbBrushEntity cbBrush = cbBrushService.getById(id);
        List<CbStandardBrushEntity> list = cbStandardBrushService.list(new QueryWrapper<CbStandardBrushEntity>().eq("bid", id));
        List<FileNameAndUrl> collect = list.stream().map(m -> {
            FileNameAndUrl fileNameAndUrl = new FileNameAndUrl();
            fileNameAndUrl.setStandardBrushName(m.getStandardBrushName());
            fileNameAndUrl.setStandardBrushVideo(m.getStandardBrushVideo());
            return fileNameAndUrl;
        }).collect(Collectors.toList());
        cbBrush.setPics(collect);
        return R.ok().put("cbBrush", cbBrush);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbbrush:save")
    public R save(@RequestBody CbBrushEntity cbBrush){
        try {
            cbBrushService.save(cbBrush);
            return R.ok();
        } catch (Exception e) {
            return R.error("该字体已存在笔法数据，请刷新页面");
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbbrush:update")
    public R update(@RequestBody CbBrushEntity cbBrush){
		cbBrushService.updateById(cbBrush);
        /*Long bid = cbBrush.getId();
        List<FileNameAndUrl> pics = cbBrush.getPics();
        List<CbStandardBrushEntity> standardBrushEntities = new ArrayList<>();
        for (int i = 1; i <= pics.size(); i++) {
            FileNameAndUrl fileNameAndUrl = pics.get(i - 1);
            CbStandardBrushEntity standardBrushEntity = new CbStandardBrushEntity();
            standardBrushEntity.setBid(bid);
            standardBrushEntity.setStandardBrushVideo(fileNameAndUrl.getStandardBrushVideo());
            standardBrushEntity.setSorder(i);
            standardBrushEntity.setStandardBrushName(fileNameAndUrl.getStandardBrushName());
            standardBrushEntities.add(standardBrushEntity);
        }
        // 先删再保存
        cbStandardBrushService.remove(new QueryWrapper<CbStandardBrushEntity>().eq("bid", bid));
        cbStandardBrushService.saveBatch(standardBrushEntities);*/
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改对应基本笔画模板")
    @PostMapping("/updateJbbhmb")
    //@RequiresPermissions("sf:cbtypeface:update")
    public R updateJbbhmb(@RequestBody CbTypefaceEntity cbTypeface){
        cbTypefaceService.updateById(cbTypeface);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbbrush:delete")
    public R delete(@RequestBody Long[] ids){
		cbBrushService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 通过字体id获取笔法以及笔法图片
     */
    @ApiOperation("通过字体id获取笔法以及笔法图片")
    @GetMapping("/getBrushByWordId/{wordId}")
    public R getBrushByWordId(@PathVariable("wordId") Long wordId){
        CbBrushEntity cbBrushEntity = cbBrushService.getBrushByWordId(wordId);
        CbTypefaceEntity typefaceEntity = cbTypefaceService.getById(wordId);
        cbBrushEntity.setFid(typefaceEntity.getBfsid());
        return R.ok().put("cbBrush", cbBrushEntity);
    }

}
